﻿using System.Collections.Generic;
using System.IO;
using Baik.Interface.Execution;
using Baik.Interface.Framework;

namespace Baik.Execution
{
    public class BuiltInObject : NonRecursingScope, IObject
    {


        public BuiltInObject(IInjector inject) : base(inject)
        {

            Dictionary = new Dictionary<string, IObject>();
            Dictionary.Add("equals", BuiltInFactory.Function(inject, BuiltinEquals));
            Dictionary.Add("toString", BuiltInFactory.Function(inject, ToIString));
        }
        private IObject ToIString(IEnumerable<IObject> arg)
        {
            var sw = new StringWriter();
            var rtn = Inject.Instantiate<IString>();
            rtn.Value = AsString();
            return rtn;
        }

        private IObject BuiltinEquals(IEnumerable<IObject> arg)
        {
            foreach (var obj in arg)
            {
                if (!((object)obj).Equals((object)this))
                {
                    return Inject.Instantiate<IFalse>();
                }

            }
            return Inject.Instantiate<ITrue>();
        }
        
        
        
        public override string AsString()
        {
            return "BUILTIN";
        }

        public string Name { get; set; }
        private int _assigned = 0;
        public void Assign()
        {
            _assigned++;
        }

        public void DeAssign()
        {
            _assigned--;
            if (_assigned < 1)
            {
                Destroy();
            }
        }




        public void Destroy()
        {
            //code goes here to remove object from memory;
        }

       
    }
}